package com.huishi.util;


import com.huishi.entity.DataSource;
import com.huishi.enums.DataXPlugin;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.FileWriter;
import java.io.IOException;

@Slf4j
public class JSONUtils {

    public static String generateDataXJob(DataSource source, DataSource target, String tableName, String maxUpdateTime) {
        // 创建主 JSON 对象
        JSONObject job = new JSONObject();

        JSONObject setting = new JSONObject();

        // 设置 speed
        JSONObject speed = new JSONObject();
        speed.put("channel", 1);
        setting.put("speed", speed);

        // 设置 errorLimit
        JSONObject errorLimit = new JSONObject();
        errorLimit.put("record", 0);
        errorLimit.put("percentage", 0.02);
        setting.put("errorLimit", errorLimit);

        // 创建 content 数组
        JSONArray content = new JSONArray();

        // 创建 reader
        JSONObject reader = new JSONObject();
        reader.put("name", DataXPlugin.getReader(source.getSourceType()));

        JSONObject readerParameter = new JSONObject();
        readerParameter.put("username", source.getUserName());
        readerParameter.put("password", source.getPassword());
        readerParameter.put("column", new JSONArray().put("*"));
        readerParameter.put("where", "NULL".equals(maxUpdateTime) ? "" : "update_time > '" + maxUpdateTime + "'");

        JSONArray readerConnection = new JSONArray();
        JSONObject readerConnectionItem = new JSONObject();
        readerConnectionItem.put("table", new JSONArray().put(tableName));
        readerConnectionItem.put("jdbcUrl", new JSONArray().put("jdbc:mysql://" + source.getIp() + ":" + source.getPort() + "/" + source.getSchema()));
        readerConnection.put(readerConnectionItem);

        readerParameter.put("connection", readerConnection);
        reader.put("parameter", readerParameter);

        // 创建 writer
        JSONObject writer = new JSONObject();
        writer.put("name", DataXPlugin.getWriter(target.getSourceType()));

        JSONObject writerParameter = new JSONObject();
        writerParameter.put("writeMode", "update");
        writerParameter.put("username", target.getUserName());
        writerParameter.put("password", target.getPassword());
        writerParameter.put("column", new JSONArray().put("*"));
        writerParameter.put("session", new JSONArray().put("set session sql_mode='ANSI'"));

        JSONArray writerConnection = new JSONArray();
        JSONObject writerConnectionItem = new JSONObject();
        writerConnectionItem.put("jdbcUrl", "jdbc:mysql://" + target.getIp() + ":" + target.getPort() + "/" + target.getSchema());
        writerConnectionItem.put("table", new JSONArray().put(tableName));
        writerConnection.put(writerConnectionItem);

        writerParameter.put("connection", writerConnection);
        writer.put("parameter", writerParameter);

        // 将 reader 和 writer 放入 content
        JSONObject contentItem = new JSONObject();
        contentItem.put("reader", reader);
        contentItem.put("writer", writer);
        content.put(contentItem);

        // 将 content 放入 job
        job.put("setting", setting);
        job.put("content", content);


        // 将 job 放入整体对象
        JSONObject output = new JSONObject();
        output.put("job", job);

        // 写入文件
        String taskName = tableName + "-" + source.getSourceType() + "-to-" + target.getSourceType();
        String fileName = "E:\\ljl\\data-tool\\jobs\\" + taskName + ".json";
        try (FileWriter file = new FileWriter(fileName)) {
            file.write(output.toString(4)); // 4 表示缩进级别
            log.debug("DataX任务JSON生成：{}", fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }


        return taskName;
    }

}
